using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class CellTable
    {
        int GAME_MINX;
        int GAME_MINY;
        int GAME_HEIGHT;
        int GAME_WIDTH;
        int CELL_WIDTH;
        int CELL_WIDTH_HALF;
        TBitmaps CARDS;
        TBitmaps MAGIC;
        TBitmaps MR;
        TButtonCenter BUTTON_MENU;
        TButtonCenter BUTTON_REPLAY;

        TBitmaps BITMAPS_HINT;
        TButtonCenter HINT_BUTTON;
        TBitmaps[] CARD_SLIPS;
        int[][] CARDINDEX;
        static int[][] CARD_SLIP_INDEX;
        static public int COUNT_DONE;
        static public int INHINT = 0;
        static public int NUMHINT = 3;
        static public string LEV = "";
        static public string Mode = ""; 
        int FAIL;
        public static CellTable instance;
        public CellTable()
        {

            //NewGame(4, 4);
            instance = this;
        }
        public void NewGame(int i1, int i2)
        {
            T.CURRENT_I1 = i1;
            T.CURRENT_I2 = i2;
            FAIL = 0;
            T.GAME_COL = 4;
            T.GAME_ROW = 4;
            switch (i1)
            {
                case 0:
                    T.GAME_COL = 4;
                    T.GAME_ROW = 4;
                    break;
                case 1:
                    T.GAME_COL = 5;
                    T.GAME_ROW = 4;
                    break;
                case 2:
                    T.GAME_COL = 6;
                    T.GAME_ROW = 5;
                    break;
                case 3:
                    T.GAME_COL = 6;
                    T.GAME_ROW = 6;
                    break;
            }

            CELL_WIDTH = (DEF.SCREEN_WIDTH - 20) / T.GAME_COL;
           // GAME_MINY = (DEF.SCREEN_HEIGHT - 50) / 2 - T.GAME_ROW * CELL_WIDTH / 2;
            //GAME_MINY = (int)(GAME_MINY * 0.8f);
            GAME_MINY = (int)(DEF.SCREEN_HEIGHT * 0.17f);
            GAME_HEIGHT = T.GAME_ROW * CELL_WIDTH;
            GAME_WIDTH = T.GAME_COL * CELL_WIDTH;
            GAME_MINX = DEF.SCREEN_WIDTH / 2 - GAME_WIDTH / 2;
            CARDS = new TBitmaps("image//card//card", 0, 15, CELL_WIDTH, -1);
            MAGIC = new TBitmaps("image//effect//effect", 0, 4, CELL_WIDTH, -1);
            BITMAPS_HINT = new TBitmaps("image//hint//hint", 0, 3, -1, (int)(DEF.SCREEN_HEIGHT * 0.2f));
            HINT_BUTTON = new TButtonCenter(BITMAPS_HINT.bitmaps[3], DEF.SCREEN_WIDTH / 2, GAME_MINY + GAME_HEIGHT + (int)(BITMAPS_HINT.height_half*1.2f), BITMAPS_HINT.scale_x);
            //MR = new TBitmaps("image//mr//menu_replay", 0, 1, -1, (int)(GAME_MINY * 0.8f));
            MR = new TBitmaps("image//mr//menu_replay", 0, 1, -1, (int)(DEF.SCREEN_HEIGHT * 0.15f));

            BUTTON_MENU = new TButtonCenter(MR.bitmaps[0], MR.width_half, GAME_MINY / 2, MR.scale_x);
            BUTTON_REPLAY = new TButtonCenter(MR.bitmaps[1], DEF.SCREEN_WIDTH - MR.width_half, GAME_MINY / 2, MR.scale_x);

            RandomCARDINDEX();

            CARD_SLIPS = new TBitmaps[5];
            int[] slipwidth = new int[5];
            slipwidth[0] = (int)(CELL_WIDTH * 0.70f);
            slipwidth[1] = (int)(CELL_WIDTH * 0.45f);
            slipwidth[2] = (int)(CELL_WIDTH * 0.25f);
            slipwidth[3] = (int)(CELL_WIDTH * 0.12f);
            slipwidth[4] = (int)(CELL_WIDTH * 0.05f);

            for (int i = 0; i < 5; i++)
            {
                CARD_SLIPS[i] = new TBitmaps("image//card//card", 0, 15, slipwidth[i], CELL_WIDTH);
            }
            CARD_SLIP_INDEX = new int[T.GAME_ROW][];
            for (int i = 0; i < T.GAME_ROW; i++)
            {
                CARD_SLIP_INDEX[i] = new int[T.GAME_COL];
            }
            COUNT_DONE = 0;
            CELL_WIDTH_HALF = CELL_WIDTH / 2;
            NUMHINT = 3;

            //SLIPP ALLL
            for (int i = 0; i < T.GAME_ROW; i++)
            {
                for (int j = 0; j < T.GAME_COL; j++)
                {
                    if (CellTable.CARD_SLIP_INDEX[i][j] != -1)
                        CellTable.CARD_SLIP_INDEX[i][j] = 1;

                }
            }
            INHINT = 35;
            SoundManager.playSound(SoundManager.SOUND_SLIPALL);
            Mode = "Mode:";
            if (T.CURRENT_I1 == 0)
                Mode += "Easy";
            if (T.CURRENT_I1 == 1)
                Mode += "Normal";
            if (T.CURRENT_I1 == 2)
                Mode += "Hard";
            if (T.CURRENT_I1 == 3)
                Mode += "Extreme";
            LEV = "LEVEL: " + (T.CURRENT_I2 + 1);

            T.TIME_BOUNS = new int[4];
            T.TIME_BOUNS[0] = 2;
            T.TIME_BOUNS[1] = 3;
            T.TIME_BOUNS[2] = 4;
            T.TIME_BOUNS[3] = 6;
        }
        int i2, i3, j3;
        public void Draw(ref SpriteBatch c)
        {
            // CARDS.DrawCenter(ref c, 0, 0, 0);
            if (INHINT > 0)
                INHINT--;
            HINT_BUTTON.Draw(ref c);
            BUTTON_MENU.Draw(ref c);
            BUTTON_REPLAY.Draw(ref c);
            c.DrawString(Game1.fontNormal, Mode, new Vector2(BUTTON_MENU.x + BUTTON_MENU.width, 0), Color.White);
            c.DrawString(Game1.fontNormal, LEV, new Vector2(BUTTON_MENU.x + BUTTON_MENU.width, Game1.fontNormal.LineSpacing), Color.White);
            c.DrawString(Game1.fontNormal, "FAIL: " + FAIL, new Vector2(BUTTON_MENU.x + BUTTON_MENU.width, 2*Game1.fontNormal.LineSpacing), Color.White);
            for (int i = 0; i < T.GAME_ROW; i++)
                for (int j = 0; j < T.GAME_COL; j++)
                {
                    //CARDS.Draw(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                    if (CARD_SLIP_INDEX[i][j] == -1)
                        continue;
                    if (CARD_SLIP_INDEX[i][j] == 0)
                    {
                        //CARDS.Draw(ref c, 15, GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                        CARDS.DrawCenter(ref c, 15, GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                    }
                    else if (CARD_SLIP_INDEX[i][j] < 9)
                    {

                        if (CARD_SLIP_INDEX[i][j] < 5)
                        {
                            i2 = CARD_SLIP_INDEX[i][j] - 1;
                            //CARD_SLIPS[i2].Draw(ref c, 15, GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                            CARD_SLIPS[i2].DrawCenter(ref c, 15, GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                        }
                        else
                        {
                            i2 = 3 - (CARD_SLIP_INDEX[i][j] - 5);
                            //CARD_SLIPS[i2].Draw(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                            CARD_SLIPS[i2].DrawCenter(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                        }
                        CARD_SLIP_INDEX[i][j]++;
                    }
                    else if (CARD_SLIP_INDEX[i][j] == 9)
                    {
                        //CARDS.Draw(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                        CARDS.DrawCenter(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);

                        if (INHINT == 1)
                        {
                            CARD_SLIP_INDEX[i][j] = 12;
                        }
                        else
                            if (INHINT == 0)
                            {
                                for (i3 = 0; i3 < T.GAME_ROW; i3++)
                                {
                                    for (j3 = 0; j3 < T.GAME_COL; j3++)
                                    {
                                        if (CARD_SLIP_INDEX[i3][j3] == 9 && (i3 != i || j3 != j))
                                        {
                                            if (CARDINDEX[i][j] == CARDINDEX[i3][j3])
                                            {
                                                //T.sounds.Play(Sound2.RIGHT);
                                                CARD_SLIP_INDEX[i3][j3] = 26;
                                                CARD_SLIP_INDEX[i][j] = 26;
                                                //CARDINDEX[i3][j3] =-1;
                                                //CARDINDEX[i][j] =-1;
                                                SoundManager.playSound(SoundManager.SOUND_RIGHT);
                                                COUNT_DONE += 2;
                                                if (COUNT_DONE == n)
                                                {
                                                    // NewGame(2, 6);
                                                    //WIN;

                                                    // T.sounds.Play(Sound2.WIN);
                                                    // 
                                                    SoundManager.playSound(SoundManager.SOUND_WIN);
                                                    if (FAIL < T.TIME_BOUNS[T.CURRENT_I1])
                                                    {
                                                        T.GAME_DATA[T.CURRENT_I1][T.CURRENT_I2] = 3;
                                                        StateWinLose.numStart = 3;
                                                    }
                                                    else if (FAIL < (int)(1.5f * T.TIME_BOUNS[T.CURRENT_I1]))
                                                    {
                                                        if (T.GAME_DATA[T.CURRENT_I1][T.CURRENT_I2] < 2) ;
                                                        T.GAME_DATA[T.CURRENT_I1][T.CURRENT_I2] = 2;
                                                        StateWinLose.numStart = 2;
                                                    }
                                                    else
                                                    {
                                                        if (T.GAME_DATA[T.CURRENT_I1][T.CURRENT_I2] < 1) ;
                                                        T.GAME_DATA[T.CURRENT_I1][T.CURRENT_I2] = 1;
                                                        StateWinLose.numStart = 1;
                                                    }
                                                    if (T.CURRENT_I1 == T.I1 && T.CURRENT_I2 == T.I2)
                                                    {
                                                        T.I2++;
                                                        if (T.I2 == 16)
                                                        {
                                                            T.I2 = 0;
                                                            T.I1++;
                                                            if (T.I1 == 4)
                                                            {
                                                                T.I1 = 3;
                                                                T.I2 = 15;
                                                            }
                                                        }
                                                    }
                                                    Game1.changeState(Game1.STATE_WINLOSE);
                                                }
                                            }
                                            else
                                            {
                                                SoundManager.playSound(SoundManager.SOUND_FAIL);

                                                CARD_SLIP_INDEX[i3][j3] = 12;
                                                CARD_SLIP_INDEX[i][j] = 12;
                                                FAIL++;
                                            }
                                        }
                                    }
                                }
                            }

                    }
                    else if (CARD_SLIP_INDEX[i][j] < 26)
                    {
                        if (CARD_SLIP_INDEX[i][j] < 18)
                            //canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j * T.CELL_WIDTH, T.GAME_MINY + i * T.CELL_WIDTH, null);
                            //CARDS.Draw(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                            CARDS.DrawCenter(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                        else if (CARD_SLIP_INDEX[i][j] < 21)
                        {
                            i2 = CARD_SLIP_INDEX[i][j] - 18;
                            //canvas.drawBitmap(CARD_SLIPS[i2][CARDINDEX[i][j]], T.GAME_MINX + j * T.CELL_WIDTH + T.CELL_WIDTH / 2 - slipwidth[i2] / 2, T.GAME_MINY + i * T.CELL_WIDTH, null);
                            //CARD_SLIPS[i2].Draw(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                            CARD_SLIPS[i2].DrawCenter(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);

                        }
                        else if (CARD_SLIP_INDEX[i][j] < 26)
                        {
                            i2 = 3 - (CARD_SLIP_INDEX[i][j] - 22);
                            //canvas.drawBitmap(CARD_SLIPS[i2][15], T.GAME_MINX + j * T.CELL_WIDTH + T.CELL_WIDTH / 2 - slipwidth[i2] / 2, T.GAME_MINY + i * T.CELL_WIDTH, null);
                            //CARD_SLIPS[i2].Draw(ref c, 15, GAME_MINX + j * CELL_WIDTH, GAME_MINY + i * CELL_WIDTH);
                            CARD_SLIPS[i2].DrawCenter(ref c, 15, GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                        }
                        CARD_SLIP_INDEX[i][j]++;
                        if (CARD_SLIP_INDEX[i][j] >= 26)
                        {
                            CARD_SLIP_INDEX[i][j] = 0;

                        }
                    }
                    else if (CARD_SLIP_INDEX[i][j] >= 26 && CARD_SLIP_INDEX[i][j] < 31)
                    {
                        //T.paint_alpha.setAlpha(255 / ((CARD_SLIP_INDEX[i][j] - 25)));

                        //canvas.drawBitmap(CARDS.bitmap[CARDINDEX[i][j]], T.GAME_MINX + j * T.CELL_WIDTH, T.GAME_MINY + i * T.CELL_WIDTH, T.paint_alpha);
                        //canvas.drawBitmap(MAGIC.bitmap[CARD_SLIP_INDEX[i][j] - 26], T.GAME_MINX + j * T.CELL_WIDTH, T.GAME_MINY + i * T.CELL_WIDTH, null);
                        CARDS.DrawCenter(ref c, CARDINDEX[i][j], GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF, 255 / ((CARD_SLIP_INDEX[i][j] - 25)));
                        MAGIC.DrawCenter(ref c, CARD_SLIP_INDEX[i][j] - 26, GAME_MINX + j * CELL_WIDTH + CELL_WIDTH_HALF, GAME_MINY + i * CELL_WIDTH + CELL_WIDTH_HALF);
                        CARD_SLIP_INDEX[i][j]++;
                        if (CARD_SLIP_INDEX[i][j] == 31)
                            CARD_SLIP_INDEX[i][j] = -1;
                    }
                }
        }
        int dx, dy;
        int count;
        int i5, j5;
        public void Touch()
        {
            if (HINT_BUTTON.Kick_First())
            {
                if (NUMHINT > 0 && INHINT == 0)
                {
                    for (int i = 0; i < T.GAME_ROW; i++)
                    {
                        for (int j = 0; j < T.GAME_COL; j++)
                        {
                            if (CellTable.CARD_SLIP_INDEX[i][j] != -1)
                                CellTable.CARD_SLIP_INDEX[i][j] = 1;

                        }
                    }
                    NUMHINT--;
                    INHINT = 35;
                    HINT_BUTTON.bitmap = BITMAPS_HINT.bitmaps[NUMHINT];
                    SoundManager.playSound(SoundManager.SOUND_SLIPALL);
                }
            }
            else if (BUTTON_MENU.Kick_First())
            {
                Game1.changeState(Game1.STATE_IGM);
                //NewGame(2, 2);
            }
            else if (BUTTON_REPLAY.Kick_First())
            {
                Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "", "ARE YOU OK?", Dialog.DIALOG_REPLAY);
                //NewGame(2, 2);

            }
            else
            {
                for (int i = 0; i <= InputState.m_currentNumPointer; i++)
                {
                    if (InputState.arraytouchPosX[i] >= GAME_MINX && InputState.arraytouchPosX[i] <= GAME_MINX + GAME_WIDTH)
                        if (InputState.arraytouchPosY[i] >= GAME_MINY && InputState.arraytouchPosY[i] <= GAME_MINY + GAME_HEIGHT)
                        {

                            dx = (InputState.arraytouchPosX[i] - GAME_MINX) / CELL_WIDTH;
                            dy = (InputState.arraytouchPosY[i] - GAME_MINY) / CELL_WIDTH;
                            if (InputState.arraytouchState[i] == TouchLocationState.Pressed)
                                SoundManager.playSound(SoundManager.SOUND_CLICK_CARD);
                            if (CARD_SLIP_INDEX[dy][dx] == 9)
                            {
                                if (INHINT == 0)
                                    CARD_SLIP_INDEX[dy][dx] = 20;
                            }
                            else
                                if (CARD_SLIP_INDEX[dy][dx] == 0)
                                {
                                    count = 0;
                                    for (i5 = 0; i5 < T.GAME_ROW; i5++)
                                    {
                                        for (j5 = 0; j5 < T.GAME_COL; j5++)
                                        {
                                            if (CARD_SLIP_INDEX[i5][j5] > 0)
                                                count++;
                                        }
                                    }
                                    if (count < 3)
                                        CARD_SLIP_INDEX[dy][dx] = 1;
                                }
                        }

                }
            }
        }
        int n;
        void RandomCARDINDEX()
        {
            Random ra = new Random();
            n = T.GAME_ROW * T.GAME_COL;
            int[] c;
            c = new int[n];
            for (int i = 0; i < n / 2; i++)
                c[i] = (int)(ra.NextDouble() * 23432) % 10;
            //c[i] = 0;
            for (int i = n / 2; i < n; i++)
                c[i] = c[i - n / 2];
            int index1;
            int index2;
            int temp;
            for (int i = 0; i < n; i++)
            {
                index1 = (int)(ra.NextDouble() * 23432) % n;
                index2 = (int)(ra.NextDouble() * 23432) % n;
                temp = c[index1];
                c[index1] = c[index2];
                c[index2] = temp;
            }
            temp = 0;
            CARDINDEX = new int[T.GAME_ROW][];
            for (int i = 0; i < T.GAME_ROW; i++)
            {
                CARDINDEX[i] = new int[T.GAME_COL];
                for (int j = 0; j < T.GAME_COL; j++)
                {
                    CARDINDEX[i][j] = c[temp];
                    temp++;
                }
            }

        }

    }
}
